<?php
/**
 * Description of PrevisionJsonService
 *
 * @author ingeni
 */
class PrevisionJsonService {
    //put your code here
    public function display(ContextExecution $p_contexte) {
        $ligneId = $p_contexte->m_dataRequest->getData('ligneId');
        Logger::$instance->addLogMessage('ligneId:'.$ligneId);
        $prevision = new Prevision();
        $prevision->ligneId = $ligneId;
        $prevision->load();
        
        
        //echo json_encode($prevision);
        $p_contexte->addDataBlockRow($prevision);
    }
    
    public function update(ContextExecution $p_contexte){
        $mode = $p_contexte->m_dataRequest->getData('mode');
        switch($mode){
            case 'equilibrer':
                $ligneId = $p_contexte->m_dataRequest->getData('ligneId');
                $prevision = new Prevision();
                $prevision->ligneId=$ligneId;
                $prevision->load();
                $requeteTotaux = "SELECT round(sum(montant),2) as total
                                FROM operation 
                                WHERE nocompte='$prevision->noCompte' and date like concat('$prevision->mois','%') and fluxid='$prevision->fluxId'";
                $listMontantTotaux = new ListDynamicObject();
                $listMontantTotaux->name = 'ListeMontantFlux';
                $listMontantTotaux->request($requeteTotaux);
                $tab=$listMontantTotaux->getData();
                $total=$tab[0];
                $prevision->montant=$total->total;
                $prevision->save();
                break;
            default:
                //
                $ligneId = $p_contexte->m_dataRequest->getData('ligneId');
                $prevision = new Prevision();
                $prevision->ligneId=$ligneId;
                $prevision->load();
                $prevision->fieldObject($p_contexte->m_dataRequest);
                $prevision->save();
        }
        
    }
    
    public function create(ContextExecution $p_contexte) {
        $prevision = new Prevision();
        $prevision->fieldObject($p_contexte->m_dataRequest);
        $prevision->save();
    
    }
    
}

?>
